package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;

import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.cndragon.R;
import com.sskj.cndragon.bean.AddressBean;
import com.sskj.cndragon.presenter.AddressManagerActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.NormalRefreshDataSource;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;


public class AddressManagerActivity extends BaseActivity<AddressManagerActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R.id.btn_add_address)
    Button btnAddAddress;

    private SlimAdapter<AddressBean> slimAdapter;
    private MVCCoolHelper<List<AddressBean>> mvcHelper;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_address_manager;
    }

    @Override
    public AddressManagerActivityPresenter getPresenter() {
        return new AddressManagerActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        toolbar.setLeftButtonOnClickListener(view -> finish());
        ClickUtil.click(300, btnAddAddress, () -> AddAddressActivity.start(this, "1"));

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 20))
                .setDividerColor(ContextCompat.getColor(this, R.color.transparent)));
        slimAdapter = SlimAdapter.create().register(R.layout.item_address, new SlimInjector<AddressBean>() {
            @Override
            public void onInject(AddressBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tv_name, data.getName())
                        .text(R.id.tv_mobile, data.getTel())
                        .checked(R.id.cb_default, data.getIsDefault().equals("0") ? false : true)
                        .text(R.id.tv_address, data.getProvince() + data.getCity() + data.getArea() + data.getDetailAddress())
                        .clicked(R.id.tv_default, v -> {
                            if (data.getIsDefault().equals("0")) {
                                mPresenter.setAddressDefault(SPUtil.get(SPConfig.ID, ""), data.getId());
                            }
                        })
                        .clicked(R.id.tv_edit, v -> {
                            AddAddressActivity.start(AddressManagerActivity.this,
                                    "2",
                                    data.getName(),
                                    data.getTel(),
                                    data.getProvince(),
                                    data.getCity(),
                                    data.getArea(),
                                    data.getDetailAddress(),
                                    data.getId());
                        })
                        .clicked(R.id.tv_del, v -> {
                            mPresenter.delAddress(data.getId());
                        });
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        NormalRefreshDataSource<List<AddressBean>> normalRefreshDataSource = new NormalRefreshDataSource<List<AddressBean>>(coolRefreshView) {
            @Override
            public Flowable<List<AddressBean>> loadData() {
                return mPresenter.getAddresses(SPUtil.get(SPConfig.ID, ""));
            }
        };
        mvcHelper.setDataSource(normalRefreshDataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AddressManagerActivity.class);
        context.startActivity(intent);
    }

    public void setDefaultSuccess() {
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

    public void delSuccess() {
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }
}
